﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace BBPA.Admin
{
    public partial class xml2goods : System.Web.UI.Page
    {
        BLL.Goods bll = new BLL.Goods();
        BLL.Brand bllBrand = new BLL.Brand();
        BLL.Goods_BuyPrice_Log bllGoods_BuyPrice_Log = new BLL.Goods_BuyPrice_Log();
        BLL.Category bllCategory = new BLL.Category();
        int allCount = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            //return;
            foreach (string fileName in ("1,2,3,4,5,6,7,8,13").Split(','))
            {
                Model.Category modelCategory = bllCategory.GetModelList("Number='" + fileName.PadLeft(2, '0') + "'")[0];
                string filepath = System.Web.HttpContext.Current.Request.MapPath("/xml/goods/" + fileName + ".xml");
                XmlDocument doc = new System.Xml.XmlDocument();
                try { doc.Load(filepath); }
                catch (InvalidCastException err) { }

                Model.Category model1 = new Model.Category();
                Model.Category model2 = new Model.Category();

                foreach (XmlNode row in doc.SelectNodes("/Table/Row"))
                {
                    XmlNodeList listCell = row.SelectNodes("Cell");
                    string Name = listCell[0].InnerText;
                    if (Name == "商品名称") continue;
                    int maybyNoCode = listCell.Count == 6 ? 0 : 1;
                    string Code =maybyNoCode==0? listCell[1].InnerText:"";
                    int BrandID = (listCell[2 - maybyNoCode] != null && listCell[2 - maybyNoCode].InnerText != "") ? getBrandID(listCell[2 - maybyNoCode].InnerText) : maybeBrandID(Name);
                    decimal BuyPrice = listCell[3 - maybyNoCode] == null ? 0 : Math.Round(DataTypePlus.requestDecimal(listCell[3 - maybyNoCode].InnerText), 2);
                    decimal SellPrice = listCell[4 - maybyNoCode] == null ? 0 : Math.Round(DataTypePlus.requestDecimal(listCell[4 - maybyNoCode].InnerText), 2);
                    decimal SuggestStorePrice = listCell[5 + maybyNoCode] == null ? 0 : Math.Round(DataTypePlus.requestDecimal(listCell[5 - maybyNoCode].InnerText), 2);
                    if (Name == "") continue;
                    Model.Goods model = new Model.Goods();
                    model.Name = Name;
                    model.ShotrName = Name;
                    model.BuyPrice = BuyPrice;
                    model.SellPrice =SellPrice;
                    model.SuggestStorePrice = SuggestStorePrice;
                    model.BrandID = BrandID;

                    model.CategoryID = modelCategory.id;
                    model.SupplierID = 0;
                    model.Code = Code;
                    model.Description = "";
                    model.IsOffShelf = false;
                    model.State = (int)EnumHelper.GoodsState.Nomal;

                    //Rank
                    List<Model.Goods> listModelGoods = bll.GetModelList("CategoryID=" + modelCategory .id+ " order by Rank desc");
                    if (listModelGoods != null && listModelGoods.Count > 0) model.Rank = listModelGoods[0].Rank + 1;
                    else model.Rank = 1;
                    //Number
                    //Model.Category modelCategory = bllCategory.GetModel(DDL_CategoryID);
                    model.Number = modelCategory.Number + model.Rank.ToString().PadLeft(4, '0');//4位

                    int GoodsID_new = bll.Add(model);


                    allCount++;
                    Response.Write(Name + " " + BuyPrice + " " + SuggestStorePrice + " " + model.BrandID + " " + allCount.ToString() + "<br>");
                }
                Response.Write(allCount.ToString());
            }
        }
        int getBrandID(string brandname_)
        {
            List<Model.Brand> listModelBrand = bllBrand.GetModelList("Name='" + brandname_ + "'");
            if (listModelBrand.Count > 0)return listModelBrand[0].id;
            else
            {
                Model.Brand modelBrand = new Model.Brand();
                modelBrand.Name = brandname_;
                return bllBrand.Add(modelBrand);
            }
        }
        int maybeBrandID(string goodsname_)
        {
            foreach (Model.Brand modelBrand in bllBrand.GetModelList(""))
            {
                if (goodsname_.IndexOf(modelBrand.Name) >= 0) return modelBrand.id;
            }
            return 0;
        }
    }
}